/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.text.json;

import org.merak.core.model.Identifiable;
import org.merak.core.text.json.jsonfiers.Jsonfier;

/**
 * JSON does not support cyclic data structures, so be careful to not jsonfy
 * cyclical structures. This class will break the rendering process when it
 * detects a cycle. The object where the cycle starts will be rendered as "null"
 * if it isn't Identifiable object. In this case, the Id will be rendered instead.
 *
 * This class follows the Decorator Pattern, adding the described behaviour for
 * other jsonfiers.
 *
 * @author fmarinho */
public class JsonfierControl extends Jsonfier {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private final Jsonfier jsonfier;

	//~ Constructor //////////////////////////////////////////////////////////////
	//****************************************************************************
	protected JsonfierControl(Jsonfier jsonfier) {
		this.jsonfier = jsonfier;
	}
	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	@Override
	public final void render(Object object,JsonContext context) throws Exception {

		// Base Case: Not Null
		if(object == null) {
			context.getBuffer().append("null");
			return;
		}

		/* Base Case: Stop when finding an already stacked object
		 * In this case, try rendering a key instead of the whole object */
		if(context.isStacked(object)) {
			this.renderIdentifier(object,context);
			return;
		}

		/* Recursive Case: render the object, ensuring the rendered objects are
		 * being stacked for recursive verification */
		context.stack(object);
		this.jsonfier.render(object,context);
		context.unstack();

	}
	//****************************************************************************
	private final void renderIdentifier(Object object,JsonContext context)
		throws Exception
	{

		if ( object instanceof Identifiable ) {
			context.render( ((Identifiable<?>)object).getId() );
		}
		else {
			context.getBuffer().append("null");
		}

	}
	//****************************************************************************

}
